<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>【DP】石子合并.md | 蓝湖畔淅淅沥沥的雨</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="说明 - 2022-05-05 本篇博客为本人原创, 原发布于CSDN, 在搭建个人博客后使用爬虫批量爬取并挂到个人博客, 出于一些技术原因博客未能完全还原到初始版本(而且我懒得修改), 在观看体验上会有一些瑕疵 ,若有需求会发布重制版总结性新博客。发布时间统一定为1111年11月11日。钦此。  1 线形石子合并 参考题目 https:&#x2F;&#x2F;www.acwing.com&#x2F;problem&#x2F;cont">
<meta property="og:type" content="article">
<meta property="og:title" content="【DP】石子合并.md">
<meta property="og:url" content="http://example.com/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/index.html">
<meta property="og:site_name" content="蓝湖畔淅淅沥沥的雨">
<meta property="og:description" content="说明 - 2022-05-05 本篇博客为本人原创, 原发布于CSDN, 在搭建个人博客后使用爬虫批量爬取并挂到个人博客, 出于一些技术原因博客未能完全还原到初始版本(而且我懒得修改), 在观看体验上会有一些瑕疵 ,若有需求会发布重制版总结性新博客。发布时间统一定为1111年11月11日。钦此。  1 线形石子合并 参考题目 https:&#x2F;&#x2F;www.acwing.com&#x2F;problem&#x2F;cont">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/20201008111934601.png">
<meta property="og:image" content="http://example.com/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/20201008111347129.png">
<meta property="article:published_time" content="1111-11-11T03:06:11.000Z">
<meta property="article:modified_time" content="2022-05-06T07:53:00.582Z">
<meta property="article:author" content="StarsWhisper">
<meta property="article:tag" content="OldBlog(Before20220505)">
<meta property="article:tag" content="DP">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/20201008111934601.png">
  
    <link rel="alternate" href="/atom.xml" title="蓝湖畔淅淅沥沥的雨" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/plugin/bganimation/bg.css">

  

  <link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css" rel="stylesheet" type="text/css">
<meta name="generator" content="Hexo 6.1.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <div class="widget-wrap mobile-header">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

        <section id="main"><article id="post-【DP】石子合并" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/" class="article-date">
  <time class="post-time" datetime="1111-11-11T03:06:11.000Z" itemprop="datePublished">
    <span class="post-month">11月</span><br/>
    <span class="post-day">11</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      【DP】石子合并.md
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E7%AE%97%E6%B3%95/">算法</a>,<a class="article-category-link" href="/categories/%E7%AE%97%E6%B3%95/DP/">DP</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="说明-2022-05-05"><a class="markdownIt-Anchor" href="#说明-2022-05-05"></a> 说明 - 2022-05-05</h2>
<p>本篇博客为本人原创, 原发布于CSDN, 在搭建个人博客后使用爬虫批量爬取并挂到个人博客, 出于一些技术原因博客未能完全还原到初始版本(而且我懒得修改), 在观看体验上会有一些瑕疵 ,若有需求会发布重制版总结性新博客。发布时间统一定为1111年11月11日。钦此。</p>
<h2 id="1-线形石子合并"><a class="markdownIt-Anchor" href="#1-线形石子合并"></a> 1 线形石子合并</h2>
<p>参考题目 <a target="_blank" rel="noopener" href="https://www.acwing.com/problem/content/284/">https://www.acwing.com/problem/content/284/</a></p>
<p>题目要求最小代价</p>
<p>大致思路为：<br />
有n组石头排成一条线，<br />
规定把第i组和第i组合并（自己合并自己），代价为0<br />
1.求所有相邻的两堆石头合并的代价<br />
2.求所有相邻的三堆石头合并的代价，之前求出的两堆石头合并的代价可以作为前提<br />
3.求所有相邻的四堆石头合并的代价，之前求出的两堆石头合并的代价和三堆石头合并的代价都可以作为前提<br />
…<br />
n-1.求相邻的n堆石头合并的代价，之前求出的【2，n-1】堆石头合并的代价都可以作为前提</p>
<p>用程序模拟出这个过程便可以求得答案<br />
.</p>
<p>.</p>
<p>使用二维数组dp记录这些代价，首先令所有的dp[i][i]=0<br />
使用一维数组sum记录从第一堆石头到第i堆石头的石头总数<br />
假设要求解dp[4][7] 只需要求<br />
sum[7]-sum[4-1] + min(dp[4] [4]+dp[5] [7],dp[4] [5]+dp[6] [7],dp[4] [6]+dp[7] [7])</p>
<img src="/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/20201008111934601.png" class="" title="看图便于理解">
<p>代码:</p>
<p>​<br />
​    #include <iostream><br />
​    #include <cstdio><br />
​    #include <cstring><br />
​    #include <algorithm><br />
​    #include <iomanip><br />
​    #include <queue><br />
​    #include <cmath><br />
​    #include <map><br />
​<br />
​    const int N=303;<br />
​    int sum[N],dp[N][N],v[N];<br />
​<br />
​    int main()<br />
​    {<br />
​        int n;<br />
​        std::cin &gt;&gt; n;<br />
​        std::cin &gt;&gt; v[1];<br />
​        sum[0]=0;<br />
​        sum[1]=v[1];<br />
​        for(int i=2; i&lt;=n; i++)<br />
​        {<br />
​            std::cin &gt;&gt; v[i];<br />
​            sum[i]=sum[i-1]+v[i];<br />
​        }<br />
​        memset(dp,1,sizeof(dp));<br />
​        for(int i=1; i&lt;=n; i++) dp[i][i]=0;<br />
​        for(int span=1; span&lt;n ;span++)<br />
​        {// span表示区间长度，如span=1表示这次循环求相邻的两堆石头，span=2表示这次循环求相邻的三堆石头，span=n-1表示求所有n堆石头合并的代价<br />
​            for(int st=1; st+span&lt;=n; st++)<br />
​            {//st表示所求区间的起点，ed表示所求区间的终点，st和ed同步增加，直到求完所有合并相邻span+1堆石头的代价<br />
​                int ed=st+span;<br />
​                for(int tmp=st; tmp&lt;ed; tmp++)//tmp为[st,ed)之间的所有整数，这一步循环用来求从st到ed的最小代价<br />
​                    dp[st][ed]=std::min(dp[st][ed],dp[st][tmp]+dp[tmp+1][ed]-sum[st-1]+sum[ed]);<br />
​            }<br />
​        }<br />
​        std::cout &lt;&lt; dp[1][n] &lt;&lt; std::endl;<br />
​        return 0;<br />
​    }</p>
<p>​</p>
<h2 id="2-环形石子合并"><a class="markdownIt-Anchor" href="#2-环形石子合并"></a> 2 环形石子合并</h2>
<p>这个知识点是我根据上方线形石子合并自己想的,大概有更优的解法吧</p>
<p>参考题目 <a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P1880">https://www.luogu.com.cn/problem/P1880</a></p>
<p><strong>把环形拆成线形</strong> 把环形的n堆石头从第1堆石头和第n堆石头之间分开,成为线形,然后再把这n堆在一条线上的石头复制一份,加到第n堆石头末尾.<br />
这样即可模拟以 <strong>每</strong> 一堆石头为起点的 <strong>线形</strong> 石子合并</p>
<p>线形石子合并中for(int st=1; st+span&lt;=n; st++)的st+span&lt;=n是为了把ed限制在ed&lt;=n,现在可以改成st&lt;=n<br />
其他地方基本与线形合并相同</p>
<p>需要注意dp数组的行数没有变成两倍,for(int tmp=st; tmp&lt;ed; tmp++)中如果出现tmp+1&gt;n<br />
若要获取dp[tmp+1][ed]的值,则获取tmp[tmp+1-n][ed-n]的值</p>
<img src="/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/20201008111347129.png" class="" title="看图便于理解">  
<p>代码:</p>
<p>​<br />
​    #include <iostream><br />
​    #include <cstdio><br />
​    #include <cstring><br />
​    #include <algorithm><br />
​    #include <iomanip><br />
​    #include <queue><br />
​    #include <cmath><br />
​    #include <map><br />
​<br />
​    const int N=103;<br />
​    int sum[N&lt;&lt;1],dp[N][N&lt;&lt;1],v[N],pd[N][N&lt;&lt;1];<br />
​<br />
​    int main()<br />
​    {<br />
​        int n;<br />
​        std::cin &gt;&gt; n;<br />
​        std::cin &gt;&gt; v[1];<br />
​        sum[0]=0;<br />
​        sum[1]=v[1];<br />
​        for(int i=2; i&lt;=n; i++)<br />
​        {<br />
​            std::cin &gt;&gt; v[i];<br />
​            sum[i]=sum[i-1]+v[i];<br />
​        }<br />
​        for(int i=1; i&lt;=n; i++)<br />
​        {<br />
​            sum[i+n]=sum[i+n-1]+v[i];<br />
​        }<br />
​        memset(dp,1,sizeof(dp));<br />
​        memset(pd,0,sizeof(pd));<br />
​        for(int i=1; i&lt;=n; i++)<br />
​        {<br />
​            dp[i][i]=0;<br />
​            pd[i][i]=0;<br />
​        }<br />
​        for(int span=1; span&lt;n ;span++)<br />
​        {<br />
​            for(int st=1; st&lt;=n; st++)<br />
​            {<br />
​                int ed=st+span;<br />
​                for(int tmp=st; tmp&lt;ed; tmp++)<br />
​                {<br />
​                    if(tmp+1&gt;n)<br />
​                    {<br />
​                        dp[st][ed]=std::min(dp[st][ed],dp[st][tmp]+dp[tmp+1-n][ed-n]-sum[st-1]+sum[ed]);<br />
​                        pd[st][ed]=std::max(pd[st][ed],pd[st][tmp]+pd[tmp+1-n][ed-n]-sum[st-1]+sum[ed]);<br />
​                    }<br />
​                    else<br />
​                    {<br />
​                        dp[st][ed]=std::min(dp[st][ed],dp[st][tmp]+dp[tmp+1][ed]-sum[st-1]+sum[ed]);<br />
​                        pd[st][ed]=std::max(pd[st][ed],pd[st][tmp]+pd[tmp+1][ed]-sum[st-1]+sum[ed]);<br />
​                    }<br />
​                }<br />
​            }<br />
​        }<br />
​        int mn=1e9,mx=0;<br />
​        for(int i=1; i&lt;=n; i++)<br />
​        {<br />
​            mn=std::min(dp[i][n+i-1],mn);<br />
​            mx=std::max(pd[i][n+i-1],mx);<br />
​        }<br />
​        std::cout &lt;&lt; mn &lt;&lt; std::endl &lt;&lt; mx;<br />
​        return 0;<br />
​    }</p>
<p>​</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/1111/11/11/%E3%80%90DP%E3%80%91%E7%9F%B3%E5%AD%90%E5%90%88%E5%B9%B6/" data-id="cl2uhoebv0011e4j3fhdb2vs1" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/DP/" rel="tag">DP</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/OldBlog-Before20220505/" rel="tag">OldBlog(Before20220505)</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/1111/11/11/%E3%80%90AC%E8%87%AA%E5%8A%A8%E6%9C%BA%20_%20trie%E6%A0%91%E3%80%91G%20-%20Word%20Puzzles%20%20%20POJ%20-%201204/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          【AC自动机 _ trie树】G - Word Puzzles   POJ - 1204.md
        
      </div>
    </a>
  
  
    <a href="/1111/11/11/%E3%80%90KM%E7%AE%97%E6%B3%95%E3%80%91B%20-%20Going%20Home%20POJ%20-%202195/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">【KM算法】B - Going Home POJ - 2195.md</div>
    </a>
  
</nav>

  
</article>



</section>
        
          <aside id="sidebar">
  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <h1 class="blog-title">蓝湖畔淅淅沥沥的雨</h1>
    <h2 class="blog-subtitle">All tragedy erased, I see only wonders...</h2>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>



    <div class="social-link">
      
        <a class="hvr-bounce-in" href="https://github.com/Wldcmzy" target="_blank" title="Github">
          Github
        </a>
      
        <a class="hvr-bounce-in" href="https://blog.csdn.net/wldcmzy" target="_blank" title="CSDN">
          CSDN
        </a>
      
        <a class="hvr-bounce-in" href="https://space.bilibili.com/83743701" target="_blank" title="bilibili(无技术和学习内容)">
          bilibili(无技术和学习内容)
        </a>
      
    </div>

    <div class="friend-link">
      <h2>友情链接</h2>
      
        <a class="hvr-bounce-in" href="https://shanamaid.github.io/" target="_blank" title="夏娜主题作者的博客">
          夏娜主题作者的博客
        </a>
      
    </div>
  </div>
</div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy;2021 - 2022 StarsWhisper<br>
      由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动 | 
      主题-<a target="_blank" rel="noopener" href="https://github.com/ShanaMaid/hexo-theme-shana">Shana</a>(但是魔改)
      
    </div>
    
  </div>
</footer>
    </div>
    

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//apps.bdimg.com/libs/wow/0.1.6/wow.min.js"></script>
<script>
new WOW().init();
</script>   


  
<link rel="stylesheet" href="/plugin/fancybox/jquery.fancybox.css">

  
<script src="/plugin/fancybox/jquery.fancybox.pack.js"></script>




  
<link rel="stylesheet" href="/plugin/galmenu/GalMenu.css">

  
<script src="/plugin/galmenu/GalMenu.js"></script>

  <div class="GalMenu GalDropDown">
      <div class="circle" id="gal">
        <div class="ring">
          
            <a href="/announcement" title="" class="menuItem">公告</a>
          
            <a href="/tags" title="" class="menuItem">标签</a>
          
            <a href="/categories" title="" class="menuItem">分类</a>
          
            <a href="/archives" title="" class="menuItem">归档</a>
          
            <a href="/knightabout" title="" class="menuItem">关于</a>
          
            <a href="/bridges" title="" class="menuItem">传送门</a>
          
        </div>
        
          <audio id="audio" src="#"></audio>
        
      </div> 
</div>
<div id="overlay" style="opacity: 1; cursor: pointer;"></div>
  <script type="text/javascript">var items = document.querySelectorAll('.menuItem');
    for (var i = 0,
    l = items.length; i < l; i++) {
      items[i].style.left = (50 - 35 * Math.cos( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%";
      items[i].style.top = (50 + 35 * Math.sin( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%"
    }</script>
<script type="text/javascript">
  $(document).ready(function() {
    $('body').GalMenu({
      'menu': 'GalDropDown'
    })
  });
</script>

  <section class="hidden-xs"> 
  <ul class="cb-slideshow"> 
    <li><span>苟利</span></li> 
    <li><span>国家</span></li> 
    <li><span>生死以</span></li> 
    <li><span>岂能</span></li> 
    <li><span>祸福</span></li> 
    <li><span>趋避之</span></li> 
  </ul>
</section>

<script src="/js/script.js"></script>




  </div>
</body>
</html>